import 'dart:core' as prefix0;
import 'dart:core';
import 'package:flutter/material.dart';
import '../../flutter_dk_form.dart';

/// 选择标签
/// [title]:标题文本
/// [hasLine]：是否有下划线（Widget最下方下划线）
/// [isRadio]：是否单选
/// [data]：标签数据
/// [onChanged]：标签状态改变回调
class ChooseLabelWidget extends StatefulWidget {
  final String title;
  final bool hasLine;
  final bool isRadio;
  final List data;
  final Function onChanged;

  ChooseLabelWidget(
      {this.title,
      this.hasLine = true,
      this.isRadio = false,
      this.data,
      this.onChanged});

  @override
  State<StatefulWidget> createState() {
    return _ChooseLabelWidgetState();
  }
}

class _ChooseLabelWidgetState extends State<ChooseLabelWidget> {
  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.white,
      child: Column(
        children: <Widget>[
          TitleWidget(
            widget.title,
            hasLine: false,
            titleTextStyle: TitleTextStyle.TitleBig,
          ),
          Container(
            width: double.infinity,
            child: Wrap(
              runAlignment: WrapAlignment.center,
              spacing: 10,
              runSpacing: 19,
              children: _getWidgetList(widget.data),
            ),
            padding: EdgeInsets.only(left: 16, right: 16),
          ),
          SizedBox(
            height: 17,
          ),
          Visibility(
              visible: widget.hasLine,
              child:
                  Divider(height: 0.5, indent: 16, color: Color(0xFFEBEBEB))),
        ],
      ),
    );
  }

  List<Widget> _getWidgetList(List data) {
    List<Widget> widgetList = List();
    if (data != null) {
      data.forEach((item) {
        widgetList.add(GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            setState(() {
              if (item.selected) {
                item.selected = false;
              } else {
                if (widget.isRadio) {
                  data.forEach((it) {
                    it.selected = false;
                  });
                  item.selected = true;
                } else {
                  item.selected = true;
                }
              }
              if (widget.onChanged != null) {
                widget.onChanged(data);
              }
            });
          },
          child: Container(
            constraints: BoxConstraints(
              minWidth: 77,
            ),
            height: 32,
            child: Text(
              item.content,
              style: TextStyle(color: Color(0xFF666666), fontSize: 12),
              textAlign: TextAlign.center,
            ),
            decoration: BoxDecoration(
                borderRadius: BorderRadius.all(
                  Radius.circular(16),
                ),
                color: item.selected ? Color(0xFFDFF9F9) : Colors.transparent,
                border: item.selected
                    ? null
                    : Border.all(color: Color(0xFFE4E8EB), width: 1)),
            padding: EdgeInsets.only(left: 10, right: 10, top: item.selected
                ? 7 : 6),
          ),
        ));
      });
    }
    return widgetList;
  }
}
